<?php
/**
 * Created by PhpStorm.
 * User: baich
 * Date: 2018/7/19
 * Time: 22:14
 */

namespace app\common;


class PayUtil
{
    public function getUrl($orderSn,$order_time,$amount,$goods_name=null){
        //$amount = 0.01;
        $data = $this->_makeParam($orderSn,$order_time,$amount,$goods_name);

        foreach ($data AS $k=>$v){
            $signparams[]=$k.'='.urlencode($v);
        }

        $query_string=implode('&',$signparams);
        return \Yii::$app->params['zjpay']['url'].$query_string;

    }

    /**
     * 生成支付接口所需要的参数字符串
     */
    private function _makeParam($orderSn,$order_time,$amount,$goods_name=null)
    {
        if(!$orderSn){
            return false;
        }

        $returnUrl = \Yii::$app->params['zjpay']['returnUrl'].$orderSn;
        $notifyUrl = \Yii::$app->params['zjpay']['notifyUrl'];

        $partnerCode = \Yii::$app->params['zjpay']['partnerCode'];
        $accountName = \Yii::$app->params['zjpay']['accountName'];
        if($goods_name)
            $extend2 = json_encode(['productName'=>$goods_name]);
        else
            $extend2 = '';
        $data = array(
            'version'     => 'v2.0',
            'charset'     => 'UTF-8',
            'currency'    => 'CNY',
            'partnerCode' => $partnerCode,
            'accountName' => $accountName,
            'orderNo'     => $orderSn,
            'orderTime'   => $order_time,
            'amount'      => $amount,
            'signType'    => '1',//认证类型，可由项目决定使用类型进行认证，可更改为变量传参
            'returnUrl'  => $returnUrl,
            'notifyUrl'    => $notifyUrl,
            'extend1'      => $returnUrl,
            'extend2'    => $extend2,
            //'signContent'=> ''
        );

        $str = "";
        foreach($data as $k => $v){
            $str .= "$k=$v&";
        }

        $str = rtrim($str,'&');
        //获取签名
        $data['signContent'] = $this->_getSignPKCS12($str);
        return $data;
    }

    public function _getSignPKCS12($str){
        $key=array();
        $key_name = \Yii::$app->params['zjpay']['partnerCode'].'.pfx';
        $pfx = __DIR__ .'/../config/'.$key_name;
        $pwd = \Yii::$app->params['zjpay']['pwd'];
        openssl_pkcs12_read(file_get_contents($pfx),$key,$pwd);
        openssl_sign($str, $sign, $key['pkey']);
        return base64_encode($sign);
    }

    public function verify($data){
        $signContent = $data['signContent'];
        unset($data['signContent']);
        unset($data['paygatewayOrderNo']);
        if(isset($data['payCardNo']))
            unset($data['payCardNo']);
        if(isset($data['thirdPartyCode']))
            unset($data['thirdPartyCode']);

        $str = "";
        foreach($data as $k => $v){
            $str .= "$k=$v&";
        }
        $str = rtrim($str,'&');

        $key_name = \Yii::$app->params['zjpay']['partnerCode'].'.crt';
        $cerpath = __DIR__ .'/../config/'.$key_name;

        $fp = fopen($cerpath, "r");
        $cert = fread($fp, 8192);
        fclose($fp);

        $key_name = \Yii::$app->params['zjpay']['partnerCode'].'.pem';
        $cerpath = __DIR__ .'/../config/'.$key_name;
        $cer_key = file_get_contents($cerpath); //获取证书内容

        $cer_key = openssl_x509_read($cer_key);

        $unsignMsg = base64_decode($signContent);
        $result = openssl_verify($str, $unsignMsg, $cer_key,2);
        if(!$result){
            //echo (openssl_error_string());exit;
            return false;
        }else{
            return true;
        }

    }

}